package quq.missq.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import quq.missq.Constants;
import quq.missq.R;
import quq.missq.activity.TopicDetailActivity;
import quq.missq.beans.MyCollectionBean;
import quq.missq.utils.UserTools;
import quq.missq.utils.VolleyTool;
import quq.missq.views.NoScrollGridView;

/**
 * Created by River on 7/27/15.
 */
public class CollectionTopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private static final int TYPE_FOOTER = 0;
    private static final int TYPE_TOPIC = 1;

    private Context mContext;
    private LinkedList<MyCollectionBean.Result> results;
    private LayoutInflater mInflater;


    public CollectionTopicAdapter(Context context){
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(LinkedList<MyCollectionBean.Result> results){
        this.results = results;
    }

    public LinkedList<MyCollectionBean.Result> getData(){
        return this.results;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        if(viewType == TYPE_FOOTER){
            View view = mInflater.inflate(R.layout.foot_layout,null);
            return new FootViewHolder(view);
        }else if(viewType == TYPE_TOPIC){
            View view = mInflater.inflate(R.layout.collection_topic_adapter,null);
            return new TopicHolder(view);
        }else{
            return null;
        }
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder instanceof TopicHolder){
            TopicHolder holder = (TopicHolder)viewHolder;
            final MyCollectionBean.Result result = results.get(position);
            holder.avtar.setImageUrl(Constants.IMAGE_HOST+result.getPost().getAuthor().getAvatar80(),VolleyTool.getImageLoader());
            holder.name.setText(result.getPost().getAuthor().getNkname());
            holder.time.setText(result.getCreated());
            holder.gridView.setAdapter(new ImageGridViewAdapter(mContext, result.getPost().getAttachs()));
            holder.content.setText(result.getPost().getTitle());
            int type = -1;
            if(result.getPost().getCategory().getId() == 5){
                type = R.mipmap.icon_qa;
            }else{
                type = R.mipmap.icon_show;
            }
            holder.typeIcon.setImageResource(type);
            holder.gridView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_UP){
                        Intent intent = new Intent();
                        intent.setClass(mContext, TopicDetailActivity.class);
                        intent.putExtra("id",result.getPost().getId());
                        mContext.startActivity(intent);
                        return false;
                    }
                    return false;
                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, TopicDetailActivity.class);
                    intent.putExtra("id",result.getPost().getId());
                    mContext.startActivity(intent);
                }
            });
            holder.quxiao.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog dialog = new AlertDialog.Builder(mContext).setTitle("提醒").setMessage("删除收藏")
                            .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).setNegativeButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    results.remove(results.get(position));
                                    CollectionTopicAdapter.this.notifyDataSetChanged();
                                    Map<String,String> params = new HashMap<>();
                                    params.put("id",result.getPost().getId()+"");
                                    params.put("acc_token", UserTools.getUser(mContext).getAcc_token());
                                    VolleyTool.get((Activity)mContext,Constants.UNFAVOR_TOPIC_URL,params,
                                            null,0,null);
                                }
                            }).show();
                }
            });
        }else if(viewHolder instanceof FootViewHolder){
            FootViewHolder footHolder = (FootViewHolder)viewHolder;
            footHolder.itemView.setTag("footView");
            footHolder.itemView.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position+1 == getItemCount()){
            return TYPE_FOOTER;
        }else{
            return TYPE_TOPIC;
        }
    }

    @Override
    public int getItemCount() {
        return results.size()+1;
    }

    class TopicHolder extends RecyclerView.ViewHolder{

        NetworkImageView avtar;
        TextView name;
        TextView time;
        NoScrollGridView gridView;
        TextView joinCount;
        TextView comment;
        TextView content;
        ImageView typeIcon;
        ImageView quxiao;

        public TopicHolder(View itemView) {
            super(itemView);
            avtar = (NetworkImageView)itemView.findViewById(R.id.avtar);
            name = (TextView)itemView.findViewById(R.id.name);
            time = (TextView)itemView.findViewById(R.id.time);
            gridView = (NoScrollGridView)itemView.findViewById(R.id.gridview);
            joinCount = (TextView)itemView.findViewById(R.id.joinCount);
            comment = (TextView)itemView.findViewById(R.id.comment);
            content = (TextView)itemView.findViewById(R.id.content);
            typeIcon = (ImageView)itemView.findViewById(R.id.typeIcon);
            quxiao = (ImageView)itemView.findViewById(R.id.quxiao);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder{

        public FootViewHolder(View itemView) {
            super(itemView);
        }

    }

}
